﻿<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Tlianmengs extends CI_Controller{
    private $data = array();
    
    function __construct() {
        parent::__construct();
       //echo md5('admin1');
        $this->load->helper('url');
        $this->load->model('Muser');
        $this->data['verification'] = $this->session->userdata('session_id');
        if($this->Muser->check_login() != '1')
        {
            redirect('user');
            exit;
        }
        
    }
    public function index()
    {
        $this->view('desktop_view','');
    }
    public function siteset()
    {
        $this->config->load('siteset',true);
        $this->data['siteset'] = $this->config->item('siteset');
        $this->db->where('isfauto',1);
        $this->data['autofnum'] = $this->db->count_all_results('wtt_autos');
        $this->db->where('israuto',1);
        $this->data['autornum'] = $this->db->count_all_results('wtt_autos');
        
        $this->view('siteset_view',  $this->data);
    }
    public function prizes()
    {
       $this->load->model('Mprize');
       $this->data['page_list']=$this->Mprize->GetPrizes();
       $this->view('prize_view',  $this->data);
    }

     public function exchange()
    {
       $this->load->model('Mexchange');
       $this->data['page_list']=$this->Mexchange->GetExchange();
       $this->view('exchange_view',  $this->data);
    }

    
    public function editprizes()
    {
       if( ! $this->chkv($this->input->post('verification'))){
            show_error("非法提交菜单");
            exit;
        }
       $this->load->model('Mprize');
       $this->Mprize->EditPrizes();
       redirect('tlianmengs/prizes');
    }
    
   public function editexchange()
    {
       if( ! $this->chkv($this->input->post('verification'))){
            show_error("非法提交菜单");
            exit;
        }
       $this->load->model('Mexchange');
       $this->Mexchange->EditExchange();
       redirect('tlianmengs/exchange');
    }

    public function save_siteset()
    {
        if( ! $this->chkv($this->input->post('verification'))){
            show_error("非法提交菜单");
            exit;
        }

        $this->load->model('Msite');
        $this->Msite->edit_site();
        redirect('tlianmengs/siteset');
    }
    
    
    public function adminuser()
    {
        $postaction = $this->uri->segment(3);
        if($postaction == 'edit')
        {
           if( ! $this->chkv($this->input->post('verification'))){
                show_error("非法提交菜单");
                exit;
            }
            $admin_id = $this->input->post('admin_id');
            $new_password = $this->input->post('password');
            $DB1 = $this->load->database('update', TRUE);
            $DB1->query("update wtt_admins set password='".md5($new_password)."' where id=".$admin_id);
            redirect("tlianmengs/adminuser");
        }elseif ($postaction == 'add') {
           if( ! $this->chkv($this->input->post('verification'))){
                show_error("非法提交菜单");
                exit;
            }
            $this->Muser->add();
            redirect("tlianmengs/adminuser");
        }elseif($postaction == 'del'){
           if( ! $this->chkv($this->uri->segment(4))){
                show_error("非法提交菜单");
                exit;
            }
            $admin_id = $this->uri->segment(5);
            $this->Muser->del($admin_id);
            redirect("tlianmengs/adminuser");
        }else{
            $query = $this->db->query('select * from wtt_admins order by id');  
            $this->data['page_list']=$query->result_array();
            $this->view('adminuser_view',  $this->data);
        }
    }
    
    public function adsfilter()
    {
        $DB1 = $this->load->database('update', TRUE);
        $postaction = $this->uri->segment(3);
        if($postaction == 'edit')
        {
           if( ! $this->chkv($this->input->post('verification'))){
                show_error("非法提交菜单");
                exit;
            }
            $admin_id = $this->input->post('admin_id');
            $new_password = $this->input->post('password');
            
            $DB1->query("update wtt_filter set strc='".($new_password)."' where id=".$admin_id);
            redirect("tlianmengs/adsfilter");
        }elseif ($postaction == 'add') {
           if( ! $this->chkv($this->input->post('verification'))){
                show_error("非法提交菜单");
                exit;
            }
            $username = $this->input->post('username');
            $new_password = $this->input->post('password');
            $sql_arr = array('strc'=>$username);
            $DB1->query($DB1->insert_string('wtt_filter',$sql_arr));
            redirect("tlianmengs/adsfilter");
        }elseif($postaction == 'del'){
           if( ! $this->chkv($this->uri->segment(4))){
                show_error("非法提交菜单");
                exit;
            }
            $admin_id = $this->uri->segment(5);
            $DB1->query("delete from  wtt_filter WHERE id=".$admin_id);
            redirect("tlianmengs/adsfilter");
        }else{
            $query = $this->db->query('select * from wtt_filter order by id');  
            $this->data['page_list']=$query->result_array();
            $this->view('adsfilter_view',  $this->data);
        }
    }
    
    /*
    **支付宝产品管理
    **
    */
    public function products()
    {
       $DB1 = $this->load->database('update', TRUE);
        $postaction = $this->uri->segment(3);
        if($postaction == 'edit')
        {
           if( ! $this->chkv($this->input->post('verification'))){
                show_error("非法提交菜单");
                exit;
            }
            $admin_id = $this->input->post('admin_id');
            $new_password = $this->input->post('password');
            $DB1->query("update wtt_admins set password='".md5($new_password)."' where id=".$admin_id);
            redirect("tlianmengs/products");
        }elseif ($postaction == 'add') {
           if( ! $this->chkv($this->input->post('verification'))){
                show_error("非法提交菜单");
                exit;
            }
            $this->Muser->padd();
            redirect("tlianmengs/products");
        }elseif($postaction == 'del'){
           if( ! $this->chkv($this->uri->segment(4))){
                show_error("非法提交菜单");
                exit;
            }
            $admin_id = $this->uri->segment(5);
            $this->Muser->pdel($admin_id);
            redirect("tlianmengs/products");
        }else{
            $query = $this->db->query('select * from wtt_products order by id');  
            $this->data['page_list']=$query->result_array();
            $this->view('products_view',  $this->data);
        } 
    }
    
    public function customer()	{
	   $this->load->model('Mcustomer');	
	   $pageindex = $this->uri->segment(3);
	   $pageSize = 20 ;
	   if(preg_match('/^[\-+]?[0-9]+$/', $pageindex) && $pageindex >= 0){
		  echo $this->Mcustomer->get_ctmers($pageindex,$pageSize);	
	   }else{
            $this->data['pagecount'] = $this->db->count_all('wtt_customers');
            $this->data['pageSize']= $pageSize;
            $this->view('customer_view',$this->data);
	   }
    }
    function get_uidc()
    {
       $p = $this->uri->segment(5);
       //$ptype = $this->input->get('ptype');
       //$nuid = $this->input->get('uid');
       $ptype = $this->uri->segment(4);
       $nuid = $this->uri->segment(3);
       $this->load->model('Mwbuser');
       if(!is_numeric($p)) $p = 1;
       //echo $nuid.$ptype;
       $logs = $this->Mwbuser->getlogs($nuid,$p,$ptype,20);
       //$ptype 0 1收入 else 2 支出   
       //1，关注，2转发 ，3 充值,4,分享 ，5邀请好友,6违规，7，违规返还  8,签到 9，抽奖 10,兑换实物 ,11 转让积分
       $str_arr = array('','关注','转发','充值','分享','邀请好友','违规','违规返还','签到','抽奖','兑换实物','转让积分');
       $r = '<table width="100%" border="0" cellspacing="0" cellpadding="0" id="sssss">';
       $r .='<tr><td>类型:<a href="'.site_url('tlianmengs/get_uidc').'/'.$nuid.'/1">支出</a> <a href="'.site_url('tlianmengs/get_uidc').'/'.$nuid.'/0">收入</a></td><td>积分</td> <td>时间</td></tr><tr>';
       if(is_array($logs)){
            //$r = '';
            foreach ($logs as $log){
                $r .='<tr><td>';
                if($ptype == '0'){
                  
                  if($log['types'] != '7'){ 
                    $r .= $str_arr[$log['types']]."了<a href='http://weibo.com/u/".$log['touid']."' style='color:#FF6600;' target='_blank'>";
                    $r .= $log['tonickname']."</a></td>";
                    
                  }else{
                    $r .= "<a href='http://weibo.com/u/".$log['touid']."' style='color:#FF6600;' target='_blank'>";
                        $r .= $log['tonickname']."</a>".$str_arr[$log['showtype']].$str_arr[$log['types']]."给你";
                  }
                  
                }else{
                   if($log['types'] != '6'){
                        $r .= "<a href='http://weibo.com/u/".$log['touid']."'  target='_blank'>".$log['tonickname']."</a>".$str_arr[$log['types']];
                        $r .= "了你";
                   }else{
                         $r .= $str_arr[$log['showtype']].$str_arr[$log['types']]."返回给<a href='http://weibo.com/u/".$log['touid']."'  target='_blank'>".$log['tonickname']."</a>";                        
                   } 
                }
                 $r .= "<td>".$log['points']."</td>";
                 $r .= "<td>".date('Y-n-j H:i:s',$log['datatime'])."</td>";
                 $r .='</tr>';
            
            }
            
        
       }
       $r .= '</tr><tr><td colspan="3" align="center">';
       $r .='<a href="'.site_url('tlianmengs/get_uidc').'/'.$nuid.'/'.$ptype.'">首页</a> <a href="'.site_url('tlianmengs/get_uidc').'/'.$nuid.'/'.$ptype.'/'.($p-1).'">上页</a>  </a> <a href="'.site_url('tlianmengs/get_uidc').'/'.$nuid.'/'.$ptype.'/'.($p+1).'">下页</a> </a> <a href="'.site_url('tlianmengs/get_uidc').'/'.$nuid.'/'.$ptype.'/20">最后一页</a></td></tr></table>';
       header("content-type: text/html; charset=utf-8");
       echo $r;
    }
    function setstatus()
    {
        if( ! $this->chkv($this->input->post('verification'))){
                show_error("非法提交菜单");
                exit;
        }
        $wb_id = $this->input->post('weiboid');
        $u_status = $this->input->post('u_status');
        if(!is_numeric($wb_id))
        {
                show_error("参数错误");
                exit;
        }
        if($u_status !='0' && $u_status !='1')
        {
                show_error("参数错误");
                exit;
        }
        $this->Muser->setactive($wb_id,$u_status);
        redirect('tlianmengs/customer');
    }
    function setaddressstatus()
    {
	if( ! $this->chkv($this->input->post('verification'))){
                show_error("非法提交菜单");
                exit;
        }
        $address_id = $this->input->post('address_id');
        $u_status = $this->input->post('u_status');
	 $order_id = $this->input->post('order_id');
       
        if($u_status !='0' && $u_status !='1')
        {
                show_error("参数错误");
                exit;
        }
	 $db1 = $this->load->database('update', TRUE);
	  $db1->query("update wtt_addres set status='". $u_status."'  where address_id=".$address_id." and order_id=".$order_id);
	if( $this->input->post('type') ==1)
	{
	   redirect('tlianmengs/makaaddress');
	}else{
	  redirect('tlianmengs/address');
	}
    }
    function chongzhi()
    {
       if( ! $this->chkv($this->input->post('verification'))){
                show_error("非法提交菜单");
                exit;
        }
        $wb_id = $this->input->post('weiboid1');
        $setjifen = $this->input->post('setjifen');
        $nickname = $this->input->post('nbc1');
        if(!is_numeric($wb_id) || !is_numeric($setjifen))
        {
                show_error("参数错误");
                exit;
        }
        //$this->Muser->setjifen($wb_id,$setjifen);
        $this->load->model('Mwbuser');
        $muid = $this->Mwbuser->getinfo($wb_id,'jifen');
        $nejifen = $muid['jifen']  < 0 ? abs($muid['jifen']) : 0;
        
        if($this->Mwbuser->setpoints($wb_id,($setjifen+$nejifen)))
        //if($this->Mwbuser->setpoints($wb_id,$setjifen))
        {
            $this->Mwbuser->insertlog($wb_id,0,$nickname,3,$setjifen,0,1);
        }
        
        
        redirect('tlianmengs/customer');
    }
    function reposts()
    {
        $this->load->model('Mcustomer');	
	$pageindex = $this->uri->segment(3);
	$pageSize = 20 ;
	if(preg_match('/^[\-+]?[0-9]+$/', $pageindex) && $pageindex >= 0){
		echo $this->Mcustomer->get_reposts($pageindex,$pageSize);	
	}else{
            $this->data['pagecount'] = $this->db->count_all('wtt_repost');
	    $this->data['pageSize']= $pageSize;
            
            $this->view('reposts_view',$this->data);
	}
    }
    
    function paylists()
    {
        $this->load->model('Mcustomer');	
	$pageindex = $this->uri->segment(3);
	$pageSize = 20 ;
	if(preg_match('/^[\-+]?[0-9]+$/', $pageindex) && $pageindex >= 0){
		echo $this->Mcustomer->get_paylist($pageindex,$pageSize);	
	}else{
            $this->data['pagecount'] = $this->db->count_all('wtt_payorder');
	    $this->data['pageSize']= $pageSize;
            
            $this->view('paylists_view',$this->data);
	}
    }
  function transferlists()
    {
        
        $this->load->model('Mcustomer');
    $pageindex = $this->uri->segment(3);
    $pageSize = 20 ;
    if(preg_match('/^[\-+]?[0-9]+$/', $pageindex) && $pageindex >= 0){
        echo $this->Mcustomer->get_transferlist($pageindex,$pageSize);   
    }else{
            $this->data['pagecount'] =  $this->Mcustomer->get_transferlist_num();
        $this->data['pageSize']= $pageSize;
            
            $this->view('transferlists_view',$this->data);
    }
    
    }

//已兑换列表
    function exchangelists()
    {
        $this->load->model('Mcustomer');
        $pageindex = $this->uri->segment(3);
        $pageSize = 20;
     
        if(preg_match('/^[\-+]?[0-9]+$/', $pageindex) && $pageindex >= 0){
            echo $this->Mcustomer->get_exchangelist($pageindex,$pageSize);   
        }else{
                $this->data['pagecount'] = $this->db->count_all('wtt_exchangeorder');
            $this->data['pageSize']= $pageSize;
                
                $this->view('exchangelists_view',$this->data);
        }

    }


    //修改订单状态
    function setorder()
    {
        if( ! $this->chkv($this->input->post('verification'))){
                show_error("非法提交菜单");
                exit;
        }
        $order_num = $this->input->post('order_num');
        if(!is_numeric($order_num))
        {
                show_error("参数错误");
                exit;
        }
         $this->db->select("a.wb_id,a.pro_id,a.order_status,b.screen_name");
                $this->db->from('wtt_payorder a');
                $this->db->join('wtt_customers b','a.wb_id=b.wb_id','left');
                $this->db->where('a.order_id',$order_num);
                //$query = $this->db->get_where("wtt_payorder",array("order_id"=>$out_trade_no));
                $query = $this->db->get();
                if($query->num_rows() > 0)
                {
                    $row = $query->row_array();
                    if($row['order_status'] == '0')
                    {
                        $wb_id = $row['wb_id'];
                        $pro_id = $row['pro_id'];
                        $screen_name = $row['screen_name'];
                        $query = $this->db->get_where("wtt_products",array("p_price"=>$pro_id));
                        if($query->num_rows() < 0)
                        {
                            show_error("您所选择的产品不存在！",200,"参数错误");
                        }else{
                            $row = $query->row_array(); 
                            $jifen = $row['p_jifen'];
                            $this->load->model('Mwbuser');
                            //$this->db->query("updatge wtt_customers set jifen=jifen+".$jifen." where wb_id=".$pro_id);
                            $muid = $this->Mwbuser->getinfo($wb_id,'jifen');
                            $nejifen = $muid['jifen']  < 0 ? abs($muid['jifen']) : 0;
                            
                            $this->Mwbuser->setpoints($wb_id,($jifen+$nejifen),1);
                            //$this->Mwbuser->setpoints($wb_id,$jifen,1);
                            $this->Mwbuser->insertlog($wb_id,0,$screen_name,3,$jifen,0,1);
                            $DB1 = $this->load->database('update', TRUE);
                            $DB1->query("update wtt_payorder set order_status=1 where order_id='".$order_num."'");
                            redirect('tlianmengs/paylists');
                        }
                        
                    }else{
                        show_error("订单已经处理，不可多次处理","200","订单错误！");
                    }
                    
                }else{
                    show_error("订单不存在","200","订单错误！");
                }
    }
    function delrepost()
    {
       if( ! $this->chkv($this->uri->segment(4))){
                show_error("非法提交菜单");
                exit;
            }
            $rp_id = $this->uri->segment(3); 
            if(!is_numeric($rp_id)) 
            {
                 show_error("非法提交菜单");
                exit;
            }
            $DB1 = $this->load->database('update', TRUE);
            $DB1->where('id',$rp_id);
            $DB1->delete('wtt_repost');
            redirect('tlianmengs/reposts');
    }
	//这里是删除5天以前的微博
    function deloldrespost()
    {
        $DB1 = $this->load->database('update', TRUE);
        $newtime = time() - 432000;
        $DB1->where('datetime < ',$newtime);
        $DB1->where('rp_status',0);
        $DB1->delete('wtt_repost');
        $arr = array (
            "status"=>1,
            "info"=> '',
            "data"=>""
            );
        header("content-type: text/html; charset=utf-8");
      echo json_encode($arr);
    }
    //删除待支付列表
    function delnopay() {
       $DB1 = $this->load->database('update', TRUE);
        $DB1->where('order_status',0);
        $DB1->delete('wtt_payorder');
        $arr = array (
            "status"=>1,
            "info"=> '',
            "data"=>""
            );
        header("content-type: text/html; charset=utf-8");
      echo json_encode($arr);
    }
    public function delmonliwai()
    {
    	$DB1 = $this->load->database('update', TRUE);
    	$DB1->where('datatime<',(time()-2592000));
    	$DB1->delete('wtt_hasfollows');
    	$arr = array (
    			"status"=>1,
    			"info"=> '',
    			"data"=>""
    	);
    	header("content-type: text/html; charset=utf-8");
    	echo json_encode($arr);
    }
    protected function chkv($vdata)
   {
       if($this->session->userdata('session_id') == $vdata)
       {
           return TRUE;
       }else{
           return FALSE;
       }
    }
    protected function view($page , $data = '', $is_heard = 0)
    {
	$this->load->view('admin/heard_view');
	if($is_heard != '1')
	{
            $this->load->view('admin/menu_view');
	}
	$this->load->view('admin/'.$page,$data);
	//$this->load->view('foot_view');
    }



    public function address()	{
	   $this->load->model('Mcustomer');	
	   $pageindex = $this->uri->segment(3);
	   $pageSize = 20 ;
	   if(preg_match('/^[\-+]?[0-9]+$/', $pageindex) && $pageindex >= 0){
		  echo $this->Mcustomer->get_address($pageindex,$pageSize);	
	   }else{
            $this->data['pagecount'] = $this->db->count_all('wtt_addres');
            $this->data['pageSize']= $pageSize;
            $this->view('address_view',$this->data);
	   }
    }
	public function makaaddress()	{
	   $this->load->model('Mcustomer');	
	   $pageindex = $this->uri->segment(3);
	   $pageSize = 20 ;
	   if(preg_match('/^[\-+]?[0-9]+$/', $pageindex) && $pageindex >= 0){
		  echo $this->Mcustomer->get_makaaddress($pageindex,$pageSize);	
	   }else{
            $this->data['pagecount'] = $this->db->count_all('wtt_addres');
            $this->data['pageSize']= $pageSize;
            $this->view('makaaddress_view',$this->data);
	   }
    }
}
